//
//  ServiceArgs.m
//  CommonLibrary
//
//  Created by aJia on 13/2/20.
//  Copyright (c) 2013年 rang. All rights reserved.
//

#import "ServiceArgs.h"

@interface ServiceArgs()

-(NSString*)paramsFormatString:(NSArray*)params;

@end

@implementation ServiceArgs
@synthesize serviceURL,serviceNameSpace;
@synthesize methodName,serviceGUID,soapMessage;
@synthesize webURL,headers,defaultSoapMesage;

#pragma mark -
#pragma mark 属性重写
-(NSString*)defaultSoapMesage{
    NSString *soapBody=@"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
    "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
    "<soap:Header>\n"
    "<WebServiceSoapHeader xmlns=\"%@\">\n"
    "<Guid>%@</Guid>\n"
    "</WebServiceSoapHeader>\n"
    "</soap:Header>\n"
    "<soap:Body>%@</soap:Body></soap:Envelope>";
    return soapBody;
}

-(NSString*)soapMessage:(NSArray*)soapParams{
    if (soapMessage) {
        return soapMessage;
    }
    return [self stringSoapMessage:soapParams];
}

-(NSMutableDictionary*)headers{
    NSMutableDictionary *dic=[NSMutableDictionary dictionary];
    [dic setValue:[[self webURL] host] forKey:@"Host"];
    [dic setValue:@"text/xml; charset=utf-8" forKey:@"Content-Type"];
    [dic setValue:[NSString stringWithFormat:@"%lu",(unsigned long)[[self soapMessage] length]] forKey:@"Content-Length"];
    [dic setValue:[NSString stringWithFormat:@"%@%@",[self serviceNameSpace],[self methodName]] forKey:@"SOAPAction"];
    return dic;
}

#pragma mark -
#pragma mark 私有方法
-(NSString*)paramsFormatString:(NSArray*)params{
    NSMutableString *xml=[NSMutableString stringWithFormat:@""];
    for (NSDictionary *item in params) {
        id key = [[item allKeys] objectAtIndex:0];
        [xml appendFormat:@"<%@>",key];
        [xml appendFormat:@"%@",[item objectForKey:key]];
        [xml appendFormat:@"</%@>",key];
    }
    return xml;
}

#pragma mark -
#pragma mark 公有方法
-(NSString*)stringSoapMessage:(NSArray*)params{
    if (params) {
        NSMutableString *soap=[NSMutableString stringWithFormat:@"<%@ xmlns=\"%@\" >",[self methodName],[self serviceNameSpace]];
        
        [soap appendString:[self paramsFormatString:params]];
        [soap appendFormat:@"</%@>",[self methodName]];
        
        return [NSString stringWithFormat:[self defaultSoapMesage],[self serviceNameSpace],[self serviceGUID],soap];
    }
    NSString *body=[NSString stringWithFormat:@"<%@ xmlns=\"%@\" />",[self methodName],[self serviceNameSpace]];
    return [NSString stringWithFormat:[self defaultSoapMesage],[self serviceNameSpace],[self serviceGUID],body];
}

-(void)serviceMethodName:(NSString*)name soapMessageArray:(NSArray*)msg
{
    self.methodName=name;
    self.soapMessage=[self stringSoapMessage:msg];
}

-(void)serviceMethodName:(NSString*)name soapParametModel:(ServiceParametModel*)msg
{
    self.methodName=name;
    self.parametModel = msg;
    self.soapMessage=[self stringSoapMessage:msg.getValueArray];
}

- (void)dealloc
{
    serviceURL = nil;
    serviceNameSpace = nil;
    methodName = nil;
    serviceGUID = nil;
    soapMessage = nil;
    webURL = nil;
    headers = nil;
    defaultSoapMesage = nil;
}

@end
